﻿/*
    创建日期: 2018.6.14
    创建者:张存
    邮箱:zhangcunliang@126.com
    说明：用于协议编码的帮助类
    修改记录:
        注册 CodePagesEncodingProvider

 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ASmile.Helpers
{
    /// <summary>
    /// 字节数组编码帮助类
    /// </summary>
    public class BytesEncodeHelper
    {
        static BytesEncodeHelper()
        {
#if NETSTANDARD
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public BytesEncodeHelper()
        {
            _ReaultList = new List<byte>();
        }

        /// <summary>
        /// 用于默认转换字符串的编码(默认为UTF-8)
        /// </summary>
        public Encoding TextEncoding { set; get; } = Encoding.UTF8;

        List<byte> _ReaultList;
        /// <summary>
        /// 获取结果列表
        /// </summary>
        public List<byte> GetResult()
        {
            return _ReaultList;
        }
        /// <summary>
        /// 清除结果
        /// </summary>
        public void ClearData()
        {
            _ReaultList.Clear();
        }
        /// <summary>
        /// 增加一个空(0)字节
        /// </summary>
        /// <param name="count">要增加的字节数</param>
        public BytesEncodeHelper AddEmptyBytes(int count)
        {
            for (int i = 0; i < count; i++)
                AddByte(0);
            return this;
        }
        /// <summary>
        /// 增加集合
        /// </summary>
        public BytesEncodeHelper AddBytes(IEnumerable<byte> data)
        {
            _ReaultList.AddRange(data);
            return this;
        }
        /// <summary>
        /// 增加一个字节数组
        /// </summary>
        public BytesEncodeHelper AddByte(byte data)
        {
            _ReaultList.Add(data);
            return this;
        }
        /// <summary>
        /// 增加一组字节数组，指定偏移量，长度（如果数组长度小于count 用0填充）
        /// </summary>
        public BytesEncodeHelper AddBytes(byte[] data, int offset, int count, bool convertLH)
        {
            if (convertLH)
            {
                data = BytesHelper.Reverse(data);
            }
            for (int i = offset; i < count; i++)
            {
                if (i < data.Length)
                {
                    AddByte(data[i]);
                }
                else
                {
                    AddByte(0);
                }
            }
            return this;
        }
        /// <summary>
        /// 设置指定位置的值
        /// </summary>
        public BytesEncodeHelper SetValue(int index, byte data)
        {
            _ReaultList[index] = data;
            return this;
        }
        /// <summary>
        /// 增加一组数组
        /// </summary>
        public BytesEncodeHelper AddBytes(byte[] data, bool convertLH)
        {
            return AddBytes(data, 0, data.Length, convertLH);
        }
        /// <summary>
        /// 增加2字节无符号数值
        /// </summary>
        public BytesEncodeHelper AddUInt16(UInt16 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加2字节有符号数值
        /// </summary>
        public BytesEncodeHelper AddInt16(Int16 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加4字节无符号数值
        /// </summary>
        public BytesEncodeHelper AddUInt32(UInt32 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加4字节有符号数值
        /// </summary>
        public BytesEncodeHelper AddInt32(Int32 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加8字节无符号数值
        /// </summary>
        public BytesEncodeHelper AddUInt64(UInt64 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加8字节有符号数值
        /// </summary>
        public BytesEncodeHelper AddInt64(Int64 data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加8字节 Double
        /// </summary>
        public BytesEncodeHelper AddDouble(double data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加4字节 Float
        /// </summary>
        public BytesEncodeHelper AddFloat(float data, bool convertLH = false)
        {
            var bs = BitConverter.GetBytes(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加16进制字符串
        /// </summary>
        public BytesEncodeHelper AddHex(string data, bool convertLH = false)
        {
            var bs = BytesHelper.HexToByteArr(data);
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加指定编码的字符串
        /// </summary>
        /// <param name="data">要增加的数据</param>
        /// <param name="encode">编码</param>
        /// <param name="fixedLength">固定长度，不足则0填充</param>
        /// <param name="convertLH">是否高低位转换</param>
        public BytesEncodeHelper AddString(string data, Encoding encode, int fixedLength, bool convertLH = false)
        {
            var bs = encode.GetBytes(data);
            if (fixedLength <= 0) fixedLength = bs.Length;
            return AddBytes(bs, 0, fixedLength, convertLH);
        }
        /// <summary>
        /// 增加一个用 UTF-8 解析的字符串，指定固定长度，不足前面补0
        /// </summary>
        /// <param name="data">要加入的字符串</param>
        /// <param name="fixedLength">最长字节数</param>
        public BytesEncodeHelper AddString(string data, int fixedLength)
        {
            return AddString(data, TextEncoding, fixedLength);
        }
        /// <summary>
        /// 增加一个默认GBK编码的字符串
        /// </summary>
        public BytesEncodeHelper AddString(string data, Encoding encode)
        {
            return AddString(data, encode, 0);
        }
        /// <summary>
        /// 按默认GBK编码加入字符串
        /// </summary>
        public BytesEncodeHelper AddString(string data)
        {
            return AddString(data, 0);
        }
        /// <summary>
        /// 增加一个 xxx.xxx.xxx.xxx 格式的IP地址，4字节
        /// </summary>
        public BytesEncodeHelper AddIpString(string ip, bool convertLH = false)
        {
            var bs = IPAddress.Parse(ip).GetAddressBytes();
            return AddBytes(bs, convertLH);
        }
        /// <summary>
        /// 增加 count 个 回车换行,
        /// </summary>
        public BytesEncodeHelper AddLine(int count = 1)
        {
            for (int i = 0; i < count; i++)
            {
                AddString("\r\n");
            }
            return this;
        }
    }
}